package com.mission.schedule.adapter;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mission.schedule.R;
import com.mission.schedule.adapter.utils.CommonAdapter;
import com.mission.schedule.adapter.utils.ViewHolder;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.qd606.CLInventoryListBean;
import com.mission.schedule.bean.qd606.NoteTitleDetailBean;
import com.mission.schedule.swipexlistview.SwipeXListView;
import com.mission.schedule.swipexlistview.SwipeXListViewNoRefesh.onRightViewWidthListener;
import com.mission.schedule.swipexlistview.SwipeXListViewNoHead;
import com.mission.schedule.swipexlistview.SwipeXListViewNoRefesh;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class QDListAdapter extends BaseAdapter implements
		onRightViewWidthListener {

	private Context context;
	private List<NoteTitleDetailBean.TDelListBean> mList;
	private Handler handler;
	private SwipeXListViewNoRefesh swipeXListView;
	int width;
	App app = App.getDBcApplication();
	boolean isMyNote = true;

	public void deletelist(int position,String titleid,int uid){
		App.getDBcApplication().updateNoteTitleState(titleid,uid+"",3,false);
		mList.remove(position);
	}

	public QDListAdapter(Context context, List<NoteTitleDetailBean.TDelListBean> lDatas,Handler handler,
						 SwipeXListViewNoRefesh swipeXlistview, int width,boolean isMyNote) {
		this.context = context;
		this.mList = lDatas;
		this.handler = handler;
		this.swipeXListView = swipeXlistview;
		this.width = width;
		this.isMyNote = isMyNote;
		swipeXListView.setonRightViewWidthListener(this);
	}

	private void setOnclick(int what,String type,int position) {
		Message message = Message.obtain();
		message.obj = type;
		message.arg2 = position;
		message.what = what;
		handler.sendMessage(message);
	}

	@Override
	public void onRightViewWidth(int position) {
		swipeXListView.setRightViewWidth(context.getResources()
				.getDimensionPixelSize(R.dimen.qdlist_item_200));
	}

	//设置双#号包裹的文字颜色
	private void setcolor(String content, TextView editText) {
		SpannableStringBuilder builder = new SpannableStringBuilder(content);
		if (content.length() > 1) {
			List<Integer> integerList = new ArrayList<>();
			int index = 0;
			int count = hit(content, "#");
			if (count > 1) {
				if (count % 2 == 0) {
					for (int i = 0; i < count; i++) {
						if (i == 0) {
							index = content.indexOf("#");
						} else {
							index = content.indexOf("#", index + 1);
						}
						integerList.add(index);
					}
					for (int i = 0; i < integerList.size(); i++) {
						if (i > 0) {
							if (i % 2 != 0) {
								ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
								builder.setSpan(span, integerList.get(i - 1), integerList.get(i) + 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
							}
						}
					}
					ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#aaaaaa"));
					int khindex = content.indexOf("(");
					builder.setSpan(span, khindex, content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					editText.setText(builder);
				} else {
					for (int i = 0; i < count - 1; i++) {
						if (i == 0) {
							index = content.indexOf("#");
						} else {
							index = content.indexOf("#", index + 1);
						}
						integerList.add(index);

					}
					for (int i = 0; i < integerList.size(); i++) {
						if (i > 0) {
							if (i % 2 != 0) {
								ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
								builder.setSpan(span, integerList.get(i - 1), integerList.get(i) + 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
							}
						}
					}
					ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#aaaaaa"));
					int khindex = content.indexOf("(");
					builder.setSpan(span, khindex, content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					editText.setText(builder);
				}
			} else {
				ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#aaaaaa"));
				int khindex = content.indexOf("(");
				builder.setSpan(span, khindex, content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				editText.setText(builder);
			}
		} else {
			ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#aaaaaa"));
			int khindex = content.indexOf("(");
			builder.setSpan(span, khindex, content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			editText.setText(builder);
		}
	}

	public int hit(String a, String b) {
		if (a.length() < b.length()) {
			return 0;
		}
		char[] a_t = a.toCharArray();
		int count = 0;

		for (int i = 0; i < a.length(); i++) {
			StringBuffer buffer = new StringBuffer();
			for (int j = 0; j < b.length(); j++) {
				buffer.append(a_t[i + j]);
			}
			if (buffer.toString().equals(b)) {
				count++;
			}
		}

		return count;
	}

	/**
	 * 删除日程
	 */
	private void deleteItem(final int position, final String titleid, final int uid) {
		final AlertDialog builder = new AlertDialog.Builder(context)
				.create();

		builder.show();
		Window window = builder.getWindow();
		WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
		params.alpha = 0.92f;
		window.setAttributes(params);// 设置生效
		window.setContentView(R.layout.dialog_alterdelete);
		TextView delete_ok = (TextView) window.findViewById(R.id.delete_ok);
		TextView delete_canel = (TextView) window
				.findViewById(R.id.delete_canel);
		delete_canel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				builder.dismiss();
			}
		});
		TextView delete_tv = (TextView) window.findViewById(R.id.delete_tv);

		delete_tv.setText("是否确认删除该条清单?");
		delete_ok.setText("确认");
		delete_ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setOnclick(1,titleid,position);
				deletelist(position,titleid,uid);
				builder.dismiss();
			}
		});
	}

	@Override
	public int getCount() {
		return mList == null ? 0 : mList.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null){
			holder = new ViewHolder();

			convertView = LayoutInflater.from(context).inflate(R.layout.adapter_qdlist,null);
			holder.tv_delete = (TextView)convertView.findViewById(R.id.tv_delete);// 删除
			holder.tv_gd = (TextView)convertView.findViewById(R.id.tv_gd);// 归档
			holder.qdll = (LinearLayout)convertView.findViewById(R.id.qdll);// 删除布局
			holder.qdstyle = (ImageView)convertView.findViewById(R.id.qdtyle);
			holder.time = (TextView)convertView.findViewById(R.id.time);
			holder.title = (TextView)convertView.findViewById(R.id.title);
			holder.share = (TextView)convertView.findViewById(R.id.share);


			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (isMyNote){
			holder.tv_gd.setText("归档");
		}else {
			holder.tv_gd.setText("取消归档");
		}

		holder.qdll.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setOnclick(2,mList.get(position).titleId,position);
			}
		});



		holder.tv_gd.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 右归档
				if (isMyNote) {
					App.getDBcApplication().updateNoteTitleFiled(mList.get(position).titleId, mList.get(position).uid, 1);
				}else {
					App.getDBcApplication().updateNoteTitleFiled(mList.get(position).titleId, mList.get(position).uid, 0);
				}
				setOnclick(4,"",0);
			}
		});

		holder.tv_delete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 右滑删除
				deleteItem(position,mList.get(position).titleId,mList.get(position).uid);
			}
		});


		if (mList != null && mList.size() > 0) {
			switch (mList.get(position).ltype) {
				case 0:
					holder.qdstyle.setBackgroundResource(R.mipmap.icon_beiwang);
					break;
				case 1:
					holder.qdstyle.setBackgroundResource(R.mipmap.icon_putong);
					break;
				case 2:
					holder.qdstyle.setBackgroundResource(R.mipmap.icon_buy);
					break;
				case 3:
					holder.qdstyle.setBackgroundResource(R.mipmap.icon_xinyuan);
					break;
				case 4:
					holder.qdstyle.setBackgroundResource(R.mipmap.icon_renwu);
					break;
			}

			int num = App.getDBcApplication().getTitleNoteDetailData(mList.get(position).uid + "", mList.get(position).titleId, false).size();
			String titlestr;
			if (mList.get(position).titles.equals("")) {
				titlestr = App.getDBcApplication().getNoteTitleDetailContent(mList.get(position).uid + "", mList.get(position).titleId);
					if (titlestr.isEmpty()){
						titlestr = "无标题";
					}else {
						if (titlestr.length() > 10){
							titlestr = titlestr.substring(0,9);
						}
					}

			} else {
				titlestr = mList.get(position).titles;
			}

			String nums;
			if (num == 0) {
				if (mList.get(position).titleId.equals("0")) {
					titlestr = "<font color='#30a9a0'>" + titlestr + "</>";
				} else {
					titlestr = "<font color='#000000'>" + titlestr + "</>";
				}
				holder.title.setText(Html.fromHtml(titlestr));
			} else {
				if (mList.get(position).styles == 0) {
					int tick = App.getDBcApplication().getNoteDetailEndstateNums(mList.get(position).titleId, mList.get(position).uid + "");
					if (num == 0) {
						nums = "";
					} else {
						nums = "<font color='#aaaaaa'>(" + tick + "/" + num + ")</>";
					}

					if (mList.get(position).titleId.equals("0")) {
						titlestr = "<font color='#30a9a0'>" + titlestr + "</><font color='#aaaaaa'>(" + num + ")</>";
						holder.title.setText(Html.fromHtml(titlestr + nums));
					} else {
						if (num == 0) {
							setcolor(titlestr, holder.title);
						} else {
							setcolor(titlestr + "(" + tick + "/" + num + ")", holder.title);
						}
					}
//				title.setText(Html.fromHtml(titlestr+nums));
				} else {
					if (num == 0) {
						nums = "<font color='#aaaaaa'>(" + num + ")</>";
					} else {
						nums = "";
					}
					if (mList.get(position).titleId.equals("0")) {
						titlestr = "<font color='#30a9a0'>" + titlestr + "</><font color='#aaaaaa'>(" + num + ")</>";
						holder.title.setText(Html.fromHtml(titlestr + nums));
					} else {
//					titlestr = "<font color='#000000'>"+titlestr+"</>";
						if (num == 0) {
							setcolor(titlestr, holder.title);
						} else {
							setcolor(titlestr + "(" + num + ")", holder.title);
						}
					}
//				title.setText(Html.fromHtml(titlestr+nums));
				}
			}
			if (mList.get(position).createTime != null) {
				holder.time.setText(mList.get(position).createTime.substring(5, mList.get(position).createTime.length() - 3).replace('T', ' '));
			}

			if (mList.get(position).nums != null) {
				if (mList.get(position).nums.equals("")) {
					holder.share.setVisibility(View.GONE);
				} else {
					holder.share.setVisibility(View.VISIBLE);
				}
			}
		}
		return convertView;
	}

	class ViewHolder{
		TextView tv_delete;// 删除
		TextView tv_gd;// 归档
		LinearLayout qdll;// 删除布局
		ImageView qdstyle;
		TextView time;
		TextView title;
		TextView share;
	}
}